package com.youuvs.feign;

import com.youuvs.result.JSONResult;
import feign.hystrix.FallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "service-movie",fallbackFactory = MovieFeignFallbackFactory.class)
@RequestMapping("/movie")
//@Repository
public interface MovieFeign {

    //根据用户信息查票
    @RequestMapping(value = "/getTicketByUserId",method = RequestMethod.GET)
    JSONResult getTicketByUserId(Long userId);

    //根据id查电影票
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    JSONResult get(@PathVariable("id")Long id);

    /**
     * 查询所有对象
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list();

    /**
     * 拿到随机id
     * @return
     */
    @RequestMapping(value = "/randomTicket",method = RequestMethod.GET)
    JSONResult randomTicket();
}


@Component
class MovieFeignFallbackFactory implements FallbackFactory<MovieFeign>{

    @Override
    public MovieFeign create(Throwable throwable) {
        return new MovieFeign() {
            @Override
            public JSONResult getTicketByUserId(Long userId) {
                return new JSONResult().setMessage("熔断辣");
            }

            @Override
            public JSONResult get(Long id) {
                return new JSONResult().setMessage("熔断辣");
            }

            @Override
            public JSONResult list() {
                return new JSONResult().setMessage("熔断辣");
            }

            @Override
            public JSONResult randomTicket() {
                return new JSONResult().setMessage("熔断辣");
            }
        };
    }
}
